﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.ComponentModel;
using MockupDesigner.Editors.TextEditors;
using System.Collections.Specialized;
using MockupDesigner.AppItems;

namespace MockupDesigner.Items
{
    [ComboBoxEditor]
    public class ComboBoxItem : ResizableItem
    {
        public static readonly DependencyProperty ItemsProperty =
           DependencyProperty.Register("Items", typeof(string[]), typeof(ComboBoxItem), new UIPropertyMetadata(new string[1]));

        public static readonly DependencyProperty SelectedIndexProperty =
           DependencyProperty.Register("SelectedIndex", typeof(int), typeof(ComboBoxItem), new UIPropertyMetadata(0));

        public static readonly DependencyProperty TextProperty =
           DependencyProperty.Register("Text", typeof(string), typeof(ComboBoxItem), new UIPropertyMetadata(string.Empty));

        public static readonly DependencyProperty ListVisibilityProperty =
            DependencyProperty.Register("ListVisibility", typeof(Visibility), typeof(ComboBoxItem));

        public ComboBoxItem()
        {
            Text = "ComboBox";
        }

        public string[] Items
        {
            get
            {
                return (string[])GetValue(ItemsProperty);
            }
            set
            {
                autochanging = true;
                SetValue(ItemsProperty, value);
                int max = 0;
                foreach (string s in value)
                {
                    if (s.Length > max)
                    {
                        max = s.Length;
                    }
                }
                if (this.Parent is DesignerItem)
                {
                    base.Height = value.Length < 2 ? 25 : 30 * value.Length + 1;
                }

                if (value.Length > 0) SelectedIndex = 0;
                if (value.Length > 1)
                {
                    ListVisibility = Visibility.Visible;
                }
                else
                {
                    ListVisibility = Visibility.Collapsed;
                }
                autochanging = false;
            }
        }

       [Category("Prop_Content")]
        public int SelectedIndex
        {
            get
            {
                return (int)GetValue(SelectedIndexProperty);
            }
            set
            {
                if (SelectedIndex == value) return;
                if (autochanging)
                {
                    SetValue(SelectedIndexProperty, value);
                }
                else
                {
                    OnPropertyChanging("SelectedIndex", SelectedIndex, value);
                    SetValue(SelectedIndexProperty, value);
                    OnPropertyChanged("SelectedIndex");
                }
            }
        }


        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                string oldVal = string.Join("\r\n", this.Items);
                if (value == oldVal)
                    return;
                OnPropertyChanging("Text", oldVal, value);
                this.Items = value.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                SetValue(TextProperty, string.Join("\r\n", this.Items.ToArray()));
                OnPropertyChanged("Text");
            }
        }

        public Visibility ListVisibility
        {
            get
            {
                return (Visibility)GetValue(ListVisibilityProperty);
            }
            set
            {
                SetValue(ListVisibilityProperty, value);
            }
        }
    }
}
